<?php
/**
 * Name
 * 2018/11/5
 * ╔═════╗                 ╔═╗ ╔═╗
 * ║ ╔═══╝                 ║ ║ ║ ║
 * ║ ╚═══╦══╦══╦═════╦═════╣ ╚═╝ ║
 * ╚═══╗ ║  ║  ║  ║  ║  ║  ╠═╗ ╔═╝
 * ╔═══╝ ║  ║  ║  ║  ║  ║  ║ ║ ║
 * ╚═════╩═════╩══╩══╩══╩══╝ ╚═╝
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class Adminer extends Authenticatable
{
    use HasRoles;
    protected $guard_name = 'web';
    protected $table = 'adminer';
    public $timestamps = false;
    protected $rememberTokenName = '';
    protected $fillable = [
        'username',
        'password',
        'realname',
        'phone',
        'status',
        'role',
        'region',
        'created_at',
        'updated_at'
    ];

    const AdministratorRole = 1;
    const ManagerRole = 2;


    public function insert($data)
    {
        return $this->insertGetId($data);
    }

    public function edit($id, $data)
    {
        if($this->find($id)){
            return DB::table($this->table)->where('id', $id)->update($data);
        }
    }

    public function del($id)
    {
        return $this->where('id', $id)->delete();
    }

    public function find($id)
    {
        return $this->where('id', $id)->first()->toArray();
    }

    public function getAll()
    {
        return $this->get()->toArray();
    }

    public function modelHasRole(){
        return $this->hasOne('App\Models\ModelHasRole','model_id','id')->select('model_id','role_id')->withDefault([
           'model_id' => -1,
           'role_id' => ''
        ]);;
    }








}